package middleware

import (
	"github.com/gin-gonic/gin"
	"hk_api/constant"
	"hk_api/utils"
)

func JWTAuthMiddleware(c *gin.Context) {
	signToken := c.Request.Header.Get("Authorization")
	if signToken == "" {
		utils.ReturnError(c, "", constant.StatusErr, "暂无权限", 1)
		c.Abort()
		return
	}
	// 校验token
	claims, err := utils.ParserToken(signToken)
	if err != nil {
		utils.ReturnError(c, "", constant.StatusErr, err.Error(), 1)
		c.Abort()
		return
	}
	// 将用户的id放在到请求的上下文c上 使用userid, _ := c.Get("userid")
	c.Set("userid", claims.Id)
	c.Next()
}
